<?php
    Yii::import('user.models.UserActiveRecord');
    /**
    * This is the model class for table "user".
    *
    * The followings are the available columns in table 'user':
    * @property string $id
    * @property string $password
    * @property string $name
    * @property string $dob
    * @property string $gender
    * @property string $website
    * @property string $image
    * @property string $created
    * @property string $address
    * @property string $city_id
    * @property string $district_id
    * @property string $status
    *
    * The followings are the available model relations:
    * @property Branch[] $branches
    * @property Comment[] $comments
    * @property Event[] $events
    * @property City $city
    * @property District $district
    * @property Post[] $posts
    * @property UserEmail[] $userEmails
    * @property Item[] $items
    * @property UserPhone[] $userPhones
    */
    class User extends UserActiveRecord
    {
        //        public $email_main;
        //        public $phone_main;

        public $upload_method = 'file';
        public $image_file;
        public $image_url;

        /**
        * Returns the static model of the specified AR class.
        * @param string $className active record class name.
        * @return User the static model class
        */
        public static function model($className=__CLASS__)
        {
            return parent::model($className);
        }

        /**
        * @return string the associated database table name
        */
        public function tableName()
        {
            return 'user';
        }

        /**
        * @return array validation rules for model attributes.
        */
        public function rules()
        {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array('password, website, image', 'length', 'max'=>255),
                array('name', 'length', 'max'=>160),
                array('gender', 'length', 'max'=>6),
                array('address', 'length', 'max'=>50),
                array('city_id, district_id', 'length', 'max'=>10),
                array('status', 'length', 'max'=>7),
                array('dob, email_main, phone_main', 'safe'),

                array('image_file', 'file', 'allowEmpty' => true),
                array('image_file', 'file', 'types'=>'jpg, gif, png', 'allowEmpty' => true),

                array('image_url', 'url', 'allowEmpty' => true, 'on' => 'update'),
                array('upload_method', 'checkUpload', 'on' => 'create'),

                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id, password, name, dob, gender, website, image, created, address, city_id, district_id, status', 'safe', 'on'=>'search'),
            );
        }

        public function checkUpload($attribute, $params)
        {
            $post = $_POST['News'];

            $uv = new CUrlValidator;
            $urlValidate = $uv->validateValue($post['image_url']);

            if($post['upload_method'] == 'url'){
                if(!$post['image_url'] || ($post['image_url'] && !$urlValidate)){
                    $this->addError('upload_method', 'Đường dẫn URl ảnh phải đúng định dạng');    
                }
            } elseif($post['upload_method'] == 'file' && !$_FILES['browse_file']['size']){
                $this->addError('upload_method', 'Bạn cần chọn 1 ảnh để upload');
            }
        }


        /**
        * @return array relational rules.
        */
        public function relations()
        {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
                'branches' => array(self::MANY_MANY, 'Branch', 'user_favourite_branch(user_id, branch_id)'),
                'comments' => array(self::MANY_MANY, 'Comment', 'user_vote_comment(user_id, comment_id)'),
                'events' => array(self::HAS_MANY, 'Event', 'user_id'),
                'city' => array(self::BELONGS_TO, 'City', 'city_id'),
                'district' => array(self::BELONGS_TO, 'District', 'district_id'),
                'posts' => array(self::MANY_MANY, 'Post', 'user_bookmark_post(user_id, handbook_id)'),
                'items' => array(self::MANY_MANY, 'Item', 'user_favourite_item(customer_id, item_id)'),
                'userEmails' => array(self::HAS_MANY, 'UserEmail', 'user_id', 'order' => 'userEmails.id ASC'),
                'userPhones' => array(self::HAS_MANY, 'UserPhone', 'user_id', 'order' => 'userPhones.id ASC'),
                'email_main' => array(self::HAS_ONE, 'UserEmail', 'user_id', 'condition' => 'email_main.is_main = 1'),
                'phone_main' => array(self::HAS_ONE, 'UserPhone', 'user_id', 'condition' => 'phone_main.is_main = 1'),
                'emailFacebook' => array(self::HAS_ONE, 'UserEmail', 'user_id', 'condition' => 'openid_service = "FACEBOOK"'),
            );
        }


        /**
        * @return array customized attribute labels (name=>label)
        */
        public function attributeLabels()
        {
            return array(
                'id' => 'ID',
                'password' => 'Password',
                'name' => 'Họ & Tên',
                'dob' => 'Ngày sinh',
                'gender' => 'Giới tính',
                'website' => 'Website',
                'image' => 'Image',
                'created' => 'Created',
                'address' => 'Địa chỉ',
                'city_id' => 'Thành phố/Tỉnh',
                'district_id' => 'Quận/Huyện',
                'status' => 'Status',
                'phone_main' => 'SĐT chính',
                'email_main' => 'Email chính',
            );
        }

        public function beforeSave(){

            if($this->isNewRecord){
                $this->created = MyDateTime::getCurrentTime();
            }

            return parent::beforeSave();
        }


        /**
        * Retrieves a list of models based on the current search/filter conditions.
        * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
        */
        public function search()
        {
            // Warning: Please modify the following code to remove attributes that
            // should not be searched.

            $criteria=new CDbCriteria;

            $criteria->compare('id',$this->id,true);
            $criteria->compare('password',$this->password,true);
            $criteria->compare('name',$this->name,true);
            $criteria->compare('dob',$this->dob,true);
            $criteria->compare('gender',$this->gender,true);
            $criteria->compare('website',$this->website,true);
            $criteria->compare('image',$this->image,true);
            $criteria->compare('created',$this->created,true);
            $criteria->compare('address',$this->address,true);
            $criteria->compare('city_id',$this->city_id,true);
            $criteria->compare('district_id',$this->district_id,true);
            $criteria->compare('status',$this->status,true);

            return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
            ));
        }
        


        public function getGenderData(){
            return array(
                'MALE' => 'Nam',
                'FEMALE' => 'Nữ',
            );
        }

        public function getGenderLabel(){
            return $this->gender ? $this->genderData[$this->gender] : NULL;
        }


        /**
        * Tên thư mục upload cha (đơn vị hàng nghìn) của user
        */
        public function getFolderNum($user_id = NULL){
            $user_id = $user_id ? $user_id : $this->id;
            for($i = 1000; $i < 100000000; $i += 1000){
                if($user_id <= $i) return $i;   
            }   
        }

        /**
        * Đường dẫn thư mục upload ảnh cho user
        */
        public function getImagePath(){ 
            return Yii::app()->params->user['path'].$this->folderNum."/{$this->id}/";
        }
        public function getAvatarPath(){
            return $this->imagePath."avatar/";
        }

        public function getAvatarUrl($type = '150', $id = false, $image = false, $noImage = false){
            $user = $id ? User::model()->findByPk($id) : $this;
            $image = $image ? $image : $this->image;
            
            if($image){
                $avatar = "/{$user->avatarPath}{$image}_{$type}.jpg";   
            }else{
                $avatar = $noImage ? $noImage : "/files/img/no_item/avatar_{$type}.jpg";   
            }
            return $avatar;
        }

        public function getAddressFull(){
            if(!$this->address) return NULL;
            return "{$this->address}, {$this->district->name}, {$this->city->name}";
        }



        public function getHashPassword($password){
            Yii::import('ext.PasswordHash');
            $hasher = new PasswordHash(8, TRUE);
            $hash = $hasher->HashPassword($password);
            return $hash;
        }

        public function checkPassword($password, $hashPassword){
            Yii::import('ext.PasswordHash');
            $hasher = new PasswordHash(8, TRUE);
            $check = $hasher->CheckPassword($password, $hashPassword);
            return $check;
        }

        public function loginAuto($id = null, $name = null){
            $id = $id ? $id : $this->id;
            $name = $name ? $name : $this->name;

            $userIdentity = new CUserIdentity($id, '');
            $userIdentity->setState('id', $id);
            $userIdentity->setState('name', $name);

            Yii::app()->user->login($userIdentity, Yii::app()->params->user['remember']);  
        }










        ///////////////////////////////// FOR API ///////////////////////////

        /**
        * Lấy mảng dữ liệu của user
        * 
        */
        public function getData(){
            $avatar = array();
            if($this->image){
                foreach(array_keys(Yii::app()->params->user_img['img']) as $size){
                    $avatar[$size] = $this->getAvatarUrl($size);   
                }
            }
            $data = array(
                'id' => $this->id,
                'name' => $this->name,
                'dob' => $this->dob,
                'gender' => $this->gender,
                'avatar' => $avatar,
                'city_id' => $this->city_id,
                'district_id' => $this->district_id,
                'address' => $this->address,
                'addressFull' => $this->addressFull,
                'created' => $this->created,
            );

            return $data;
        }
}